package cn.xd.base.persistence.repository.impl;


import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.xd.base.basic.type.AdminRoleId;
import cn.xd.base.domain.entity.AdminOrgRole;
import cn.xd.base.domain.entity.AdminRole;
import cn.xd.base.domain.entity.AdminRoleMenu;
import cn.xd.base.domain.repository.AdminRoleRepository;
import cn.xd.base.persistence.dos.*;
import cn.xd.base.persistence.mps.*;
import cn.xd.core.ddd.db.DbRepositorySupport;
import cn.xd.core.ddd.diff.EntityDiff;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class AdminRoleRepositoryImpl extends DbRepositorySupport<AdminRole, AdminRoleId> implements AdminRoleRepository {
    @Resource
    private AdminRoleMpService adminRoleMpService;
    @Resource
    private AdminRoleMenuMpService adminRoleMenuMpService;
    @Resource
    private AdminOrgRoleMpService adminOrgRoleMpService;
    @Resource
    private AdminMenuMpService adminMenuMpService;
    @Resource
    private AdminOrgMpService adminOrgMpService;

    @Override
    protected void onInsert(AdminRole aggregate) {
        adminRoleMpService.getAdminRole(aggregate.getName().getValue(), null).isNotEmptyThrow("存在同名的角色");
        adminRoleMpService.saveResult(aggregate, AdminRoleConverter.INSTANCE).successAndFailThrow((item) -> {
            for (AdminRoleMenu adminMenu : aggregate.getMenus()) {
                adminMenuMpService.getResult(adminMenu.getMenuId(), AdminMenuConverter.INSTANCE).isEmptyThrow("未找到菜单");
                adminMenu.setRoleId(AdminRoleId.of(item.getId()));
                adminRoleMenuMpService.saveResult(adminMenu, AdminRoleMenuConverter.INSTANCE);
            }
            for (AdminOrgRole adminOrgRole : aggregate.getOrgs()) {
                adminOrgMpService.getResult(adminOrgRole.getOrgId(), AdminOrgConverter.INSTANCE).isEmptyThrow("未找到机构");
                adminOrgRole.setRoleId(AdminRoleId.of(item.getId()));
                adminOrgRoleMpService.saveResult(adminOrgRole, AdminOrgRoleConverter.INSTANCE);
            }
        });
    }

    @Override
    protected AdminRole onSelect(AdminRoleId id) {
        AdminRole adminRole = adminRoleMpService.getResult(id, AdminRoleConverter.INSTANCE).get();
        if (ObjUtil.isEmpty(adminRole)) {
            return adminRole;
        }
        List<AdminRoleMenuDO> menus = (List<AdminRoleMenuDO>) adminRoleMenuMpService.getAdminRoleMenu(adminRole.getId()).get();
        adminRole.setMenus(AdminRoleMenuConverter.INSTANCE.fromData(menus));

        List<AdminOrgRoleDO> orgs = (List<AdminOrgRoleDO>) adminOrgRoleMpService.getAdminOrgRole(adminRole.getId()).get();
        adminRole.setOrgs(AdminOrgRoleConverter.INSTANCE.fromData(orgs));
        return adminRole;
    }

    @Override
    protected void onUpdate(AdminRole aggregate, EntityDiff diff) {
        if (diff.isSelfModified()) {
            AdminRoleDO bean = diff.getBean(AdminRoleDO.class);
            if (StrUtil.isNotBlank(bean.getName())) {
                adminRoleMpService.getAdminRole(aggregate.getName().getValue(), aggregate.getId().getValue()).isNotEmptyThrow("存在同名的角色");
            }
            adminRoleMpService.updateResult(bean);
        }
        updateList(AdminRole::getMenus, diff, AdminRoleMenuConverter.INSTANCE);
        updateList(AdminRole::getOrgs, diff, AdminOrgRoleConverter.INSTANCE);

    }

    @Override
    protected void onDelete(AdminRole aggregate) {
        aggregate.delete();
        adminRoleMpService.updateResult(AdminRoleConverter.INSTANCE.toData(aggregate));
    }
}
